home *** CD-ROM | disk | FTP | other *** search
- # include "defs.h"
-
- /*
- * Given a string of the form "name=value", record the name and value
- * in the macro list. This routine just parses the string;
- * smacro() does the recording.
- */
- setmacro (s)
- char *s; /* Input string */
- {
- char *mname; /* Pointer to name part */
- char *mvalue; /* Pointer to value part */
-
- /*
- * Find the '='
- */
- mname = s;
- while (*s != EOS && *s != '=')
- s++;
- if (*s == EOS)
- {
- goof ("internal error: \"%s\" not a macro\n",
- mname);
- return;
- }
- /*
- * Replace the '=' with EOS, to null-terminate the name.
- * The value begins in the next char, and is already null-terminated.
- */
- *s = EOS;
- mvalue = s + 1; /* OK for this to be EOS */
-
- smacro (mname, mvalue);
-
- *s = '='; /* Put back the '=', it must be there later. */
- }
-
- /*
- * Record a macro
- */
- smacro (mname, mvalue)
- char *mname; /* Macro's name */
- char *mvalue; /* Macro's value */
- {
- int i; /* Loop counter */
- extern char *malloc ();
-
- /*
- * See if the macro is already recorded
- */
- for (i = 0; i < MACROMAX && macrolist[i].mname != NULL; i++)
- {
- if (strcmp (macrolist[i].mname, mname) == 0)
- break; /* Found it */
- }
-
- if (i == MACROMAX)
- {
- goof ("Too many macros (max %d).\n", MACROMAX);
- return;
- }
-
- /*
- * If the macro was not already recorded,
- * save its name in permanent storage.
- */
- if (macrolist[i].mname == NULL)
- {
- macrolist[i].mname = malloc ((unsigned) strlen (mname) + 1);
- if (macrolist[i].mname == NULL)
- nomemory ();
- strcpy (macrolist[i].mname, mname);
- }
-
- /*
- * If the macro had a previous value,
- * reclaim the storage space and assign the new value.
- */
- if (macrolist[i].mvalue != NULL)
- free (macrolist[i].mvalue);
- macrolist[i].mvalue = malloc ((unsigned) strlen (mvalue) + 1);
- if (macrolist[i].mvalue == NULL)
- nomemory ();
- strcpy (macrolist[i].mvalue, mvalue);
- }
-
- /*
- * Look up a macro name and return its value.
- * It is permissible for the value to be an empty string;
- * but if the macro is not recorded at all, return NULL.
- */
- char *getmacro (mname)
- char *mname;
- {
- int i;
-
- for (i = 0; i < MACROMAX && macrolist[i].mname != NULL; i++)
- if (strcmp (mname, macrolist[i].mname) == 0)
- return (macrolist[i].mvalue);
- return (NULL);
- }
-